package es.cazadoresmadridejos.beans.facade.payment;

import java.math.BigDecimal;

import org.directwebremoting.annotations.RemoteProxy;
import org.directwebremoting.spring.SpringCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import es.cazadoresmadridejos.beans.manager.payment.PaymentManager;
import es.cazadoresmadridejos.model.Partner;
import es.cazadoresmadridejos.model.Payment;
import es.cazadoresmadridejos.model.Reason;
import es.cazadoresmadridejos.model.Partner.HunterType;

@Service
@RemoteProxy(name="paymentFacade",creator=SpringCreator.class)
public class PaymentFacade {

	@Autowired private PaymentManager paymentManager;

	public void savePayment(
			Long paymentId,
			Long reasonId,
			String paymentType,
			Long partnerId,
			Double paymentValue){
		Payment payment;
		if(paymentId == null){
			payment = new Payment();
			payment.setReason(new Reason());
			payment.getReason().setId(reasonId);
			payment.setType(HunterType.valueOf(paymentType));
			payment.setPartner(new Partner());
			payment.getPartner().setId(partnerId);
			if(paymentValue != null){
				payment.setPayment(BigDecimal.valueOf(paymentValue));
			}
		}else{
			payment = paymentManager.get(paymentId);
			if(paymentValue != null){
				payment.setPayment(BigDecimal.valueOf(paymentValue));
			}
		}

		paymentManager.save(payment);
	}
}
